package InterviewOfCollections;

import java.util.*;

/*
 * Author: JacksonCoder
 * FileName: BasicOfArray
 * Version: 1.0.0
 */
public class BasicOfArray {
    public static void main(String[] args) {
//        int[] arr = new int[10];
//        for (int i = 0; i < arr.length; i++) {
//            arr[i] = i + 1;
//        }
//        System.out.println(Arrays.toString(arr));
//
//        HashMap<String, String> map = new HashMap<>();
//        for (int i = 1; i < 100; i++) {
//            map.put(i + "", i + "");
//        }
//        map.forEach((k, v) -> System.out.println(k + " " + v));
//        printArray();
//        changeListAndArray();
//        ArrayListExample();
//        printLinkedList();
//        VectorExample();
//        ListCompare();
        LinkedListExample();

    }

    public static void LinkedListExample() {
        // 创建一个 LinkedList
        LinkedList<Integer> linkedList = new LinkedList<>();

        // 头插法
        System.out.println("进行头插法操作...");
        headInsert(linkedList, 10);
        headInsert(linkedList, 20);
        headInsert(linkedList, 30);
        System.out.println("链表内容（头插法后）: " + linkedList);

        // 尾插法
        System.out.println("\n进行尾插法操作...");
        tailInsert(linkedList, 40);
        tailInsert(linkedList, 50);
        tailInsert(linkedList, 60);
        System.out.println("链表内容（尾插法后）: " + linkedList);

        // 打印链表的元素
        System.out.println("\n按顺序遍历链表...");
        for (int value : linkedList) {
            System.out.print(value + " ");
        }
    }

    // 头插法：在链表的头部插入元素
    public static void headInsert(LinkedList<Integer> list, int value) {
        list.addFirst(value);
        System.out.println("插入 " + value + " 到链表头部");
    }

    // 尾插法：在链表的尾部插入元素
    public static void tailInsert(LinkedList<Integer> list, int value) {
        list.addLast(value);
        System.out.println("插入 " + value + " 到链表尾部");
    }

    public static void ListCompare() {
        // test arrayList
        ArrayList<Integer> arrayList = new ArrayList<>();
        long startTime1 = System.nanoTime();
        for (int i = 0; i < 1000000; i++) {
            arrayList.add(i);
        }
        long endTime1 = System.nanoTime();
        System.out.println("ArrayList add time: " + (endTime1 - startTime1) + "ns");

        // 测试 LinkedList
        List<Integer> linkedList = new LinkedList<>();
        long startTime2 = System.nanoTime();
        for (int i = 0; i < 1000000; i++) {
            linkedList.add(i); // 添加操作
        }
        long endTime2 = System.nanoTime();
        System.out.println("LinkedList 添加时间: " + (endTime2 - startTime2) + "ns");

        // 随机访问测试
        long startTime3 = System.nanoTime();
        arrayList.get(500000);
        long endTime3 = System.nanoTime();
        System.out.println("ArrayList 随机访问时间: " + (endTime3 - startTime3) + "ns");

        long startTime4 = System.nanoTime();
        linkedList.get(500000);
        long endTime4 = System.nanoTime();
        System.out.println("LinkedList 随机访问时间: " + (endTime4 - startTime4) + "ns");
    }

    public static void VectorExample() {
        Vector<Integer> vector = new Vector<>();
        vector.add(1);
        vector.add(2);
        vector.add(3);

        System.out.println("vector: \t" + vector);
        vector.remove(1);
        System.out.println("After vector: \t" + vector);
    }

    public static void printLinkedList() {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        System.out.println("LinkedList: \t" + list);

        list.removeFirst();
        list.removeLast();

        System.out.println("after LinkedList: \t" + list);

    }

    public static void ArrayListExample() {
        List<Integer> list = new ArrayList<>();

        // add
        list.add(1);
        list.add(10);
        list.add(100);
        list.add(1000);
        list.add(10000);

        // get
        System.out.println(list.get(0));

        // modify
        list.set(2, 10086);
        System.out.println("修改后的列表: " + list);

        // delete
        list.remove(list.size() - 1);
        System.out.println("删除后的列表: " + list);

        // loop
        for (Integer num : list) {
            System.out.println(num);
        }

        // 判断是否包含 某元素
        System.out.println("是否包含 10086 ： " + list.contains(10086));

        // clear list
        list.clear();
        System.out.println("清空后的列表: " + list);


    }

    public static void changeListAndArray() {
        int[] arr = {1, 2, 3, 4, 5, 6};
        List<Integer> list = new ArrayList<>();
        for (int num : arr) {
            list.add(num);
        }
        System.out.println(list);

        //
        List<Integer> list1 = Arrays.asList(1, 2, 3);
        int[] array = list.stream().mapToInt(Integer::intValue).toArray();
        System.out.println(Arrays.toString(array));


    }

    public static void printArray() {
        int[] nums = {10, 20, 30, 40, 50};

        System.out.println("length of nums: " + nums.length);

        System.out.println("loop of nums: ");
        for (int num : nums) {
            System.out.println(num);
        }

        // modify element
        nums[2] = 320;
        System.out.println("after modify nums: " + Arrays.toString(nums));

        // dyhamic array
        int[] dynamicArray = new int[3];
        dynamicArray[0] = 1;
        dynamicArray[1] = 2;
        dynamicArray[2] = 3;
        System.out.println("dynamicArray: " + Arrays.toString(dynamicArray));
    }
}
